package com.developer.platform.application.command;

import cn.hutool.core.bean.BeanUtil;
import com.developer.platform.application.command.cmd.DictDataAdd;
import com.developer.platform.application.command.cmd.DictDataUpdate;
import com.developer.platform.application.command.cmd.DictTypeAdd;
import com.developer.platform.application.command.cmd.DictTypeUpdate;
import com.developer.platform.domain.DictData;
import com.developer.platform.domain.DictType;
import com.developer.platform.domain.service.IDictService;
import com.veeker.core.exceptions.BusinessException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Component
public class DictCmdManager {

    private final IDictService dictService;


    public DictCmdManager(IDictService dictService) {
        this.dictService = dictService;
    }

    public void saveType(DictTypeAdd dictTypeAdd){
        DictType dictType = new DictType();
        BeanUtil.copyProperties(dictTypeAdd, dictType);
        dictService.saveType(dictType);
    }

    public void saveData(DictDataAdd dictDataAdd){
        DictData dictData = new DictData();
        BeanUtil.copyProperties(dictDataAdd, dictData);
        dictService.saveData(dictData);
    }

    public void updateType(long id , DictTypeUpdate dictTypeUpdate){
        DictType dictType = new DictType(id);
        BeanUtil.copyProperties(dictTypeUpdate, dictType);
        dictService.updateType(dictType);
    }

    public void updateData(long id , DictDataUpdate dictDataUpdate){
        DictData dictData = new DictData(id);
        BeanUtil.copyProperties(dictDataUpdate, dictData);
        dictService.updateData(dictData);
    }

    @Transactional(rollbackFor = Exception.class, noRollbackFor = BusinessException.class)
    public void deleteType(String type){
        dictService.deleteType(type);
    }

    public void deleteData(Long id){
        dictService.deleteData(id);
    }


}
